package com.ansion.baseui.base.util;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;

import com.ansion.log.VLog;
import com.ansion.utils.UiUtils;

/**
 * @author john 2023-02-20
 * @desc 用户字母头像drawable生成，用于获取通知头像
 */
public class AvatarDrawable extends Drawable {

    //文本画笔
    private final TextPaint textPaint;

    //文本内容
    private final String text;
    //背景色
    private final int backgroundColor;
    //文字大小
    private final int fontSize;
    //圆角弧度
    private final float radius;


    //用于显示文本的StaticLayout
    private StaticLayout textLayout;
    private float textWidth;
    private float textHeight;
    private float textLeft;

    private Paint backgroundPaint;

    private AvatarDrawable(Builder builder) {

        // shape properties
        radius = builder.radius;

        // text and color
        text = builder.toUpperCase ? builder.text.toUpperCase() : builder.text;
        backgroundColor = builder.backgroundColor;

        // text paint settings
        if (builder.fontSize != -1)
            fontSize = builder.fontSize;
        else {
            fontSize = UiUtils.dp(18);
        }
        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(builder.textColor);
        textPaint.setAntiAlias(true);
        textPaint.setFakeBoldText(builder.isBold);
        if (builder.font != null)
            textPaint.setTypeface(builder.font);


        // drawable paint color
        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setColor(backgroundColor);

        textPaint.setTextSize(fontSize);
        try {
            textLayout = new StaticLayout(text, textPaint, UiUtils.dp(100), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
            if (textLayout.getLineCount() > 0) {
                textLeft = textLayout.getLineLeft(0);
                textWidth = textLayout.getLineWidth(0);
                textHeight = textLayout.getLineBottom(0);
            }
        } catch (Exception e) {
            VLog.eonly(e);
        }

    }


    @Override
    public void draw(Canvas canvas) {

        Rect r = getBounds();
        if (r == null)
            return;


        canvas.save();
        canvas.translate(r.left, r.top);
        //默认画正方形，最后再加圆角
//        canvas.drawRect(new RectF(r), backgroundPaint);
        canvas.drawRoundRect(new RectF(r),radius,radius,backgroundPaint);


        // draw text
        int size = r.width();
        if (textLayout != null) {
            float scale = size / (float) UiUtils.dp(50);
            canvas.scale(scale, scale, size / 2f, size / 2f);
            canvas.translate((size - textWidth) / 2 - textLeft, (size - textHeight) / 2);
            textLayout.draw(canvas);
        }

        canvas.restore();

    }


    @Override
    public void setAlpha(int alpha) {
        textPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        textPaint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public int getIntrinsicWidth() {
        return 0;
    }

    @Override
    public int getIntrinsicHeight() {
        return 0;
    }

    public static Builder builder() {
        return new Builder();
    }


    public static class Builder implements IConfigBuilder {

        //文字
        private String text;

        //背景颜色
        private int backgroundColor;

        //边框宽度
        private int borderWidth;
        //边框颜色
        private int borderColor;

        //文字字体
        private Typeface font;
        //形状
//        private RectShape shape;
        //文本颜色
        public int textColor;
        //文本字体大小
        private int fontSize;
        //是否加粗
        private boolean isBold;
        //是否转大写
        private boolean toUpperCase;
        //边角弧度
        public float radius;

        private Builder() {
            text = "";
            backgroundColor = Color.GRAY;
            textColor = Color.WHITE;
            borderWidth = 0;
            fontSize = -1;
            isBold = false;
            toUpperCase = false;
        }

        @Override
        public IConfigBuilder backgroundColor(int backgroundColor) {
            this.backgroundColor = backgroundColor;
            return this;
        }

        @Override
        public IConfigBuilder text(String text) {
            this.text = text;
            return this;
        }

        public IConfigBuilder textColor(int color) {
            this.textColor = color;
            return this;
        }


        public IConfigBuilder useFont(Typeface font) {
            this.font = font;
            return this;
        }

        public IConfigBuilder fontSize(int size) {
            this.fontSize = size;
            return this;
        }

        public IConfigBuilder bold() {
            this.isBold = true;
            return this;
        }

        public IConfigBuilder toUpperCase() {
            this.toUpperCase = true;
            return this;
        }

        @Override
        public IConfigBuilder radius(float radius) {
            this.radius = radius;
            return this;
        }


        @Override
        public AvatarDrawable build() {
            return new AvatarDrawable(this);
        }
    }

    public interface IConfigBuilder {

        public IConfigBuilder text(String text);

        public IConfigBuilder backgroundColor(int backgroundColor);

        public IConfigBuilder textColor(int color);


        public IConfigBuilder useFont(Typeface font);

        public IConfigBuilder fontSize(int size);

        public IConfigBuilder radius(float radius);

        public IConfigBuilder bold();


        public IConfigBuilder toUpperCase();


        public AvatarDrawable build();
    }


}